<template>
  <div class="staff-editor">
    <div class="container">
      <el-page-header @back="goBack" title="返回管理后台">
        <template #content>
          <span class="page-title">团队成员管理</span>
        </template>
      </el-page-header>
      
      <div class="editor-content">
        <el-row :gutter="20">
          <el-col :xs="24" :lg="16">
            <el-card>
              <template #header>
                <div class="card-header">
                  <h2>{{ isEditing ? '编辑成员' : '添加成员' }}</h2>
                  <el-button type="primary" @click="saveStaff" :loading="saving">
                    {{ isEditing ? '更新' : '保存' }}
                  </el-button>
                </div>
              </template>
              
              <el-form :model="staffForm" :rules="formRules" ref="formRef" label-width="100px">
                <el-form-item label="成员姓名" prop="name">
                  <el-input v-model="staffForm.name" placeholder="请输入成员姓名" />
                </el-form-item>
                
                <el-form-item label="成员角色" prop="role">
                  <el-select v-model="staffForm.role" placeholder="请选择角色" style="width: 100%;">
                    <el-option label="服务器管理员" value="服务器管理员" />
                    <el-option label="游戏管理员" value="游戏管理员" />
                    <el-option label="技术员" value="技术员" />
                    <el-option label="客服" value="客服" />
                    <el-option label="活动策划" value="活动策划" />
                  </el-select>
                </el-form-item>
                
                <el-form-item label="头像URL" prop="avatarUrl">
                  <el-input v-model="staffForm.avatarUrl" placeholder="请输入头像URL" />
                  <div class="avatar-preview" v-if="staffForm.avatarUrl">
                    <el-avatar :size="80" :src="staffForm.avatarUrl" />
                  </div>
                </el-form-item>
                
                <el-form-item label="成员简介" prop="bio">
                  <el-input
                    v-model="staffForm.bio"
                    type="textarea"
                    :rows="4"
                    placeholder="请输入成员简介"
                  />
                </el-form-item>
              </el-form>
            </el-card>
          </el-col>
          
          <el-col :xs="24" :lg="8">
            <el-card>
              <template #header>
                <h3>成员列表</h3>
              </template>
              
              <div class="staff-list">
                <div 
                  v-for="staff in staffList" 
                  :key="staff.id"
                  class="staff-item"
                  :class="{ active: selectedStaff?.id === staff.id }"
                  @click="selectStaff(staff)"
                >
                  <div class="staff-avatar">
                    <el-avatar :size="50" :src="staff.avatarUrl" />
                  </div>
                  <div class="staff-info">
                    <h4>{{ staff.name }}</h4>
                    <p class="role">{{ staff.role }}</p>
                  </div>
                  <div class="staff-actions">
                    <el-button type="primary" size="small" @click.stop="editStaff(staff)">
                      编辑
                    </el-button>
                    <el-button type="danger" size="small" @click.stop="deleteStaff(staff.id)">
                      删除
                    </el-button>
                  </div>
                </div>
              </div>
              
              <div class="add-new">
                <el-button type="success" @click="addNewStaff" style="width: 100%;">
                  添加新成员
                </el-button>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getStaffData } from '../../api/serverApi'

export default {
  name: 'StaffEditor',
  setup() {
    const router = useRouter()
    const formRef = ref()
    const staffList = ref([])
    const selectedStaff = ref(null)
    const isEditing = ref(false)
    const saving = ref(false)
    
    const staffForm = ref({
      id: null,
      name: '',
      role: '',
      avatarUrl: '',
      bio: ''
    })
    
    const formRules = {
      name: [
        { required: true, message: '请输入成员姓名', trigger: 'blur' }
      ],
      role: [
        { required: true, message: '请选择成员角色', trigger: 'change' }
      ],
      bio: [
        { required: true, message: '请输入成员简介', trigger: 'blur' }
      ]
    }
    
    const goBack = () => {
      router.push('/admin')
    }
    
    const loadStaff = () => {
      staffList.value = getStaffData()
    }
    
    const addNewStaff = () => {
      isEditing.value = false
      selectedStaff.value = null
      staffForm.value = {
        id: Date.now(),
        name: '',
        role: '',
        avatarUrl: '',
        bio: ''
      }
    }
    
    const selectStaff = (staff) => {
      selectedStaff.value = staff
    }
    
    const editStaff = (staff) => {
      isEditing.value = true
      selectedStaff.value = staff
      staffForm.value = { ...staff }
    }
    
    const deleteStaff = async (id) => {
      try {
        await ElMessageBox.confirm('确定要删除这个成员吗？', '确认删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        staffList.value = staffList.value.filter(item => item.id !== id)
        if (selectedStaff.value?.id === id) {
          selectedStaff.value = null
          addNewStaff()
        }
        
        localStorage.setItem('staffData', JSON.stringify(staffList.value))
        ElMessage.success('成员删除成功')
      } catch (error) {
        // 用户取消删除
      }
    }
    
    const saveStaff = async () => {
      if (!formRef.value) return
      
      try {
        await formRef.value.validate()
        saving.value = true
        
        if (isEditing.value) {
          const index = staffList.value.findIndex(item => item.id === staffForm.value.id)
          if (index !== -1) {
            staffList.value[index] = { ...staffForm.value }
          }
          ElMessage.success('成员信息更新成功')
        } else {
          staffList.value.unshift({ ...staffForm.value })
          ElMessage.success('成员添加成功')
        }
        
        localStorage.setItem('staffData', JSON.stringify(staffList.value))
        addNewStaff()
      } catch (error) {
        console.error('保存失败:', error)
      } finally {
        saving.value = false
      }
    }
    
    onMounted(() => {
      loadStaff()
      addNewStaff()
    })
    
    return {
      formRef,
      staffList,
      selectedStaff,
      isEditing,
      saving,
      staffForm,
      formRules,
      goBack,
      addNewStaff,
      selectStaff,
      editStaff,
      deleteStaff,
      saveStaff
    }
  }
}
</script>

<style scoped>
.staff-editor {
  padding: 20px 0;
}

.page-title {
  font-size: 1.5rem;
  font-weight: bold;
  color: #4CAF50;
}

.editor-content {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  color: #4CAF50;
  margin: 0;
}

.avatar-preview {
  margin-top: 10px;
  text-align: center;
}

.staff-list {
  max-height: 400px;
  overflow-y: auto;
}

.staff-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px;
  border: 1px solid #eee;
  border-radius: 8px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.3s;
}

.staff-item:hover {
  border-color: #4CAF50;
  background: #f9f9f9;
}

.staff-item.active {
  border-color: #4CAF50;
  background: #e8f5e8;
}

.staff-info {
  flex: 1;
}

.staff-info h4 {
  margin: 0 0 5px 0;
  color: #333;
  font-size: 1rem;
}

.staff-info .role {
  margin: 0;
  color: #666;
  font-size: 0.9rem;
}

.staff-actions {
  display: flex;
  gap: 5px;
}

.add-new {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

@media (max-width: 768px) {
  .card-header {
    flex-direction: column;
    gap: 15px;
  }
  
  .staff-item {
    flex-direction: column;
    text-align: center;
    gap: 10px;
  }
  
  .staff-actions {
    justify-content: center;
  }
}
</style> 